package org.consumer.client;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import org.consumer.rpcrequest.RpcRequest;

/**
 * BIO客户端
 * 
 * @author Typhoon
 *
 */
public class Client {
	
	
	public static Object connectAndGet(String host,int port,RpcRequest request) throws Exception {
		
		Socket client = new Socket(host, port);
		
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(client.getOutputStream());
		objectOutputStream.writeUTF(request.getInterfaceName());
		objectOutputStream.writeUTF(request.getMethodName());
		objectOutputStream.writeObject(request.getParameterTypes());
		objectOutputStream.writeObject(request.getParameters());
		objectOutputStream.flush();
		
		ObjectInputStream objectInputStream = new ObjectInputStream(client.getInputStream());
		Object obj = objectInputStream.readObject();
		objectOutputStream.close();
		objectInputStream.close();
		client.close();
		return obj;
	}

}
